package com.isoft.illustrationsdraw.controller;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.isoft.illustrationsdraw.service.CommentService;
import com.isoft.illustrationsdraw.service.FileService;
import com.isoft.illustrationsdraw.service.FollowService;
import com.isoft.illustrationsdraw.service.JWTService;
import com.isoft.illustrationsdraw.service.UserDTOService;
import com.isoft.illustrationsdraw.service.UserService;
import com.isoft.illustrationsdraw.service.ReportService;
import com.isoft.illustrationsdraw.service.SubLevelPricingService;
import com.isoft.illustrationsdraw.service.TagService;
import com.isoft.illustrationsdraw.service.CommissionInfoService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.isoft.illustrationsdraw.Constants;
import com.isoft.illustrationsdraw.dto.CommentDTO;
import com.isoft.illustrationsdraw.dto.UserDetailDTO;
import com.isoft.illustrationsdraw.entity.Comment;
import com.isoft.illustrationsdraw.entity.CommissionInfo;
import com.isoft.illustrationsdraw.entity.Follow;
import com.isoft.illustrationsdraw.entity.LikeIllustration;
import com.isoft.illustrationsdraw.entity.LikeTag;
import com.isoft.illustrationsdraw.entity.User;
import com.isoft.illustrationsdraw.entity.Report;
import com.isoft.illustrationsdraw.entity.SubLevel;
import com.isoft.illustrationsdraw.entity.SubLevelPricing;
import com.isoft.illustrationsdraw.service.LikeTagService;
import com.isoft.illustrationsdraw.service.SubLevelService;








@RestController
@RequestMapping("/api/user")
public class UserController {

    
    private final UserDTOService userDTOService;
    private final JWTService jwtService;
    private final UserService userService;
    private final CommentService commentService;
    private final ReportService reportService;
    private final TagService tagService;
    private final LikeTagService likeTagService;
    private final FileService fileService;
    private final FollowService followService;
    private final CommissionInfoService commissionInfoService;
    private final SubLevelPricingService subLevelPricingService;
    private final SubLevelService subLevelService;

    public UserController(SubLevelService subLevelService, SubLevelPricingService subLevelPricingService, CommissionInfoService commissionInfoService, FollowService followService, FileService fileService, UserDTOService userDTOService, JWTService jwtService, UserService userService, CommentService commentService, ReportService reportService, TagService tagService, LikeTagService likeTagService) {
        this.subLevelService = subLevelService;
        this.userDTOService = userDTOService;
        this.subLevelPricingService = subLevelPricingService;
        this.fileService = fileService;
        this.jwtService = jwtService;
        this.userService = userService;
        this.commentService = commentService;
        this.reportService = reportService;
        this.tagService = tagService;
        this.likeTagService = likeTagService;
        this.followService = followService;
        this.commissionInfoService = commissionInfoService;
    }


    
    @GetMapping("/briefinfo")
    public ResponseEntity<?> getBriefUserInfo(@RequestParam String token, @RequestParam(required = false) Long id) {
        
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        } 
        
        if (id == null) {
            return ResponseEntity.ok(userDTOService.getBriefUserInfoById(jwtService.getUserIdFromToken(token)));
        }

        return ResponseEntity.ok(userDTOService.getBriefUserInfoById(id));
    }

    // 改变用户信息
    @PostMapping("/changedetail")
    public ResponseEntity<?> changeDetail(@RequestParam String token, @RequestBody User user) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        user.setId(jwtService.getUserIdFromToken(token));
        if (userService.updateById(user)) {
            return ResponseEntity.ok("Your change has been successful!");
        }
        
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                     .body("Error message or stack trace here");
    }
    

    // 获取用户作品
    @GetMapping("/{targetUserId}/works")
    // 1875813276512485381
    public ResponseEntity<?> getUserWorks(@RequestParam String token, @PathVariable Long targetUserId, @RequestParam Integer page) {
        // 验证Token有效性
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Constants.Error.INVALID_TOKEN); // 401 Unauthorized
        }

        // 参数校验
        if (targetUserId == null || page < 1) {
            return ResponseEntity.badRequest().body("Invalid userId or page number.");
        }

        Long userId = jwtService.getUserIdFromToken(token);        

        // 分页逻辑
        try {
            return ResponseEntity.ok(userDTOService.getUserWorks(userId, targetUserId, page, null));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error retrieving user works: " + e.getMessage());
        }
    }

    // 获取用户喜欢的作品
    @GetMapping("/{targetUserId}/likes")
    public ResponseEntity<?> getUserLikes(@RequestParam String token, @PathVariable Long targetUserId, @RequestParam Integer page) {
        // 验证Token有效性
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Constants.Error.INVALID_TOKEN); // 401 Unauthorized
        }

        // 参数校验
        if (targetUserId == null || page < 1) {
            return ResponseEntity.badRequest().body("Invalid userId or page number.");
        }

        Long userId = jwtService.getUserIdFromToken(token);

        // 分页逻辑
        try {
            return ResponseEntity.ok(userDTOService.getUserLikes(userId, targetUserId, page));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error retrieving user likes: " + e.getMessage());
        }
    }

    @GetMapping("/fans")
    public ResponseEntity<?> getUserFans(@RequestParam String token, @RequestParam Long userId, @RequestParam Integer page) {
        // 验证Token有效性
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Constants.Error.INVALID_TOKEN); // 401 Unauthorized
        }

        // 参数校验
        if (userId == null || page < 1) {
            return ResponseEntity.badRequest().body("Invalid userId or page number.");
        }

        // 分页逻辑
        try {
            return ResponseEntity.ok(userDTOService.getUserFans(userId, page));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing user fans: " + e.getMessage());
        }
    }

    @GetMapping("/followed")
    public ResponseEntity<?> getFollowedByUser(@RequestParam String token, @RequestParam(required = false) Long userId, @RequestParam Integer page) {
        // 验证Token有效性
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Constants.Error.INVALID_TOKEN); // 401 Unauthorized
        }
        if (userId == null) {
            userId = jwtService.getUserIdFromToken(token);
        }
        
        // 参数校验
        if ( page < 1) {
            return ResponseEntity.badRequest().body("Invalid or page number.");
        }

        // 分页逻辑
        try {
            return ResponseEntity.ok(userDTOService.getFollowedByUser(userId, page));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing user fans: " + e.getMessage());
        }
    }

    @PostMapping("/befan")
    public ResponseEntity<?> addFanForUser(@RequestParam String token, @RequestBody String newFollowInfo) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Constants.Error.INVALID_TOKEN); // 401 Unauthorized
        }
        try {
            Long userId = jwtService.getUserIdFromToken(token);
            ObjectMapper objectMapper = new ObjectMapper();
            Follow newFollow = objectMapper.readValue(newFollowInfo, Follow.class);
            newFollow.setFollowerId(userId);
            if (userDTOService.addFanForUser(newFollow)) {
                return ResponseEntity.ok("Be a good fan!");
            }
            return ResponseEntity.badRequest().body("Some info is invalid");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @PostMapping("/bepassersby")
    public ResponseEntity<?> removeFanForUser(@RequestParam String token, @RequestBody String newFollowInfo) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Constants.Error.INVALID_TOKEN); // 401 Unauthorized
        }

        try {
            Long userId = jwtService.getUserIdFromToken(token);
            ObjectMapper objectMapper = new ObjectMapper();
            Follow newFollow = objectMapper.readValue(newFollowInfo, Follow.class);
            newFollow.setFollowerId(userId);
            if (userDTOService.removeFanForUser(newFollow)) {
                return ResponseEntity.ok("Hope you're all well.");
            }
            return ResponseEntity.badRequest().body("Some info is invalid");

        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }

    }

    @PostMapping("/addlike")
    public ResponseEntity<?> addLikeForUser(@RequestParam String token, @RequestBody String newLike) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Constants.Error.INVALID_TOKEN); // 401 Unauthorized
        }
        try {
            Long userId = jwtService.getUserIdFromToken(token);
            ObjectMapper objectMapper = new ObjectMapper();
            LikeIllustration newLikeIllustration = objectMapper.readValue(newLike, LikeIllustration.class);
            newLikeIllustration.setUserId(userId);
            if (userDTOService.addLikeForUser(newLikeIllustration)) {
                return ResponseEntity.ok("That's a good illustration!");
            }
            return ResponseEntity.badRequest().body("Some info is invalid");
        } catch (IllegalArgumentException | IOException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    @PostMapping("/removelike")
    public ResponseEntity<?> removeLikeForUser(@RequestParam String token, @RequestBody String newLike) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Constants.Error.INVALID_TOKEN); // 401 Unauthorized
        }

        try {
            Long userId = jwtService.getUserIdFromToken(token);
            ObjectMapper objectMapper = new ObjectMapper();
            LikeIllustration newLikeIllustration = objectMapper.readValue(newLike, LikeIllustration.class);
            newLikeIllustration.setUserId(userId);
            if (userDTOService.removeLikeForUser(newLikeIllustration)) {
                return ResponseEntity.ok("Done..");
            }
            return ResponseEntity.badRequest().body("Some info is invalid");
        } catch (IllegalArgumentException | IOException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
        
    }

    @PostMapping("/post-comment")
    public ResponseEntity<?> postNewComment(@RequestParam String token, @RequestBody String newComment) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Constants.Error.INVALID_TOKEN); // 401 Unauthorized
        }
        try {
            Long userId = jwtService.getUserIdFromToken(token);
            ObjectMapper objectMapper = new ObjectMapper();
            var newCommentInfo = objectMapper.readValue(newComment, CommentDTO.class);
            newCommentInfo.setCommenterId(userId);
            var result = userDTOService.addComment(newCommentInfo);
            if (result != null) {
                return ResponseEntity.ok(newCommentInfo);
            }
            return ResponseEntity.badRequest().body("Some info is invalid, or you are banned");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @PostMapping("/delete-comment")
    public ResponseEntity<?> deleteComment(@RequestParam String token, @RequestBody String Comment) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Constants.Error.INVALID_TOKEN); // 401 Unauthorized
        }
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            CommentDTO newCommentInfo = objectMapper.readValue(Comment, CommentDTO.class);
            Comment target = new Comment(newCommentInfo.getCommentId(), null, newCommentInfo.getCommenterId(), null, null);
            


            Long userId = jwtService.getUserIdFromToken(token);
            if (!userId.equals(target.getUserId())) {
                return ResponseEntity.badRequest().body("You can only delete the comment that you posted.");
            }
            if (commentService.removeById(target)) {
                return ResponseEntity.ok("Done");
            }
            return ResponseEntity.badRequest().body("Some info is invalid");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
    
    @PostMapping("/post-report")
    public ResponseEntity<?> handleReport(@RequestParam String token, @RequestBody String report) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        try {
            Long userId = jwtService.getUserIdFromToken(token);
            ObjectMapper objectMapper = new ObjectMapper();
            var newReport = objectMapper.readValue(report, Report.class);
            newReport.setReporterId(userId);
            reportService.creatNewReport(newReport);
            return ResponseEntity.ok("We've received your report");
            
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    
    }
    
    @PostMapping("/change-info")
    public ResponseEntity<?> changeUserInfo(@RequestParam String token, @RequestBody String newInfo) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            User newUserInfo = objectMapper.readValue(newInfo, User.class);
            User forSafe = new User();
            forSafe.setId(jwtService.getUserIdFromToken(token));
            forSafe.setDescription(newUserInfo.getDescription());
            forSafe.setNickname(newUserInfo.getNickname());
            if (userService.updateById(forSafe)) {
                return ResponseEntity.ok("Your change has been applied.");
            }
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Something went wrong.");

        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/add-like-tag")
    public ResponseEntity<?> addLikeTag(@RequestParam String token, @RequestParam String tag) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        Long userId = jwtService.getUserIdFromToken(token);
        Long tagId = tagService.getTagIdByName(tag);
        // LikeTag newLike = new LikeTag(null, userId, tagId);
        LikeTag newLike = new LikeTag();
        newLike.setUserId(userId);
        newLike.setTagId(tagId);
        if (likeTagService.save(newLike)) {
            return ResponseEntity.ok("Enjoy this tag");
        }
        return ResponseEntity.internalServerError().body("Something went wrong");
    }

    @GetMapping("/remove-like-tag")
    public ResponseEntity<?> reniveLikeTag(@RequestParam String token, @RequestParam String tag) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        Long userId = jwtService.getUserIdFromToken(token);
        
        var oldLike = likeTagService.getUserLikeTag(userId, tag, null);
        likeTagService.removeById(oldLike);

        return ResponseEntity.ok("delete tag successfully");
    }

    @GetMapping("/get-like-tag-status")
    public ResponseEntity<?> handleGetLikeTag(@RequestParam String token, @RequestParam String tagName) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        Long userId = jwtService.getUserIdFromToken(token);
        return ResponseEntity.ok(likeTagService.getUserLikeTag(userId, tagName, null) != null);
    }

    @GetMapping("/detail/{userId}")
    public ResponseEntity<?> getUserDetail(@RequestParam String token, @PathVariable Long userId) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        var user = userService.getById(userId);
        if (user == null) {
            return ResponseEntity.badRequest().body("User not found!");
        }
        user.setProfileImg(user.getProfileImg() == null ? null : fileService.convertToOnlinePath(user.getProfileImg()));
        Long currentUserId = jwtService.getUserIdFromToken(token);
        var t = new Follow();
        t.setFollowerId(currentUserId);
        t.setFollowedId(userId);

        UserDetailDTO result = new UserDetailDTO(user, user.getId().equals(currentUserId) ? true : false, followService.findFollow(t) == null ? false : true);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/change-commission-status")
    public ResponseEntity<?> changeCommissionStatus(@RequestParam String token, @RequestParam boolean status) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        Long userId = jwtService.getUserIdFromToken(token);
        User updateCommission = new User();
        updateCommission.setId(userId);
        updateCommission.setAcceptsCommissions(status);
        if (userService.updateById(updateCommission)) {
            return ResponseEntity.ok("Commission status updated");
        }
        return ResponseEntity.internalServerError().body("Something went wrong");
    }

    @GetMapping("/get-sub-info/{userId}")
    public ResponseEntity<?> handleGetSubInfo(@RequestParam String token, @PathVariable Long userId) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        var subInfo = subLevelPricingService.getSub(userId);
        return ResponseEntity.ok(subInfo);
    }

    @PostMapping("/post-commission-info")
    public ResponseEntity<?> handlePostCommissionInfo(@RequestParam String token, @RequestBody String commissionInfo) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            CommissionInfo newCommissionInfo = objectMapper.readValue(commissionInfo, CommissionInfo.class);
            
            Long userId = jwtService.getUserIdFromToken(token);
            newCommissionInfo.setUserId(userId);
            var t = commissionInfoService.getCommissionInfoByUserId(userId);

            if (t == null) {
                commissionInfoService.save(newCommissionInfo);
                return ResponseEntity.ok(newCommissionInfo);
            } else {
                t.setDescription(newCommissionInfo.getDescription());
                t.setEstimatedDescription(newCommissionInfo.getEstimatedDescription());
                commissionInfoService.updateById(t);
                return ResponseEntity.ok(t);
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/get-my-id")
    public ResponseEntity<?> handleGetId(@RequestParam String token) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        Long userId = jwtService.getUserIdFromToken(token);
        Map<String, String> response = new HashMap<>();
        response.put("id", userId.toString());  // 将 userId 转换为字符串并放入 Map
        
        // 返回包含键值对的响应体
        return ResponseEntity.ok(response);
    }

    @PostMapping("/change-commission")
    public ResponseEntity<?> handleChangeCommission(@RequestParam String token, @RequestBody(required = false) String newInfo) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        try {
            Long userId = jwtService.getUserIdFromToken(token);
            ObjectMapper objectMapper = new ObjectMapper();
            CommissionInfo newCommissionInfo = objectMapper.readValue(newInfo, CommissionInfo.class);
            newCommissionInfo.setUserId(userId);
            var oldInfo =  commissionInfoService.getCommissionInfoByUserId(userId);
            if (oldInfo != null) {
                newCommissionInfo.setId(oldInfo.getId());
                commissionInfoService.updateById(newCommissionInfo);
            } else {
                commissionInfoService.save(newCommissionInfo);
            }
            return ResponseEntity.ok(newCommissionInfo);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } 
    }

    @GetMapping("/get-recommand")
    public ResponseEntity<?> handleRecommand(@RequestParam String token, @RequestParam(required = false) Long illustrationId) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        Long userId = jwtService.getUserIdFromToken(token);
        if (illustrationId != null) {
            return ResponseEntity.ok(userDTOService.getRecommandIllustrationByIllustrationId(userId, illustrationId));
        } 
        return ResponseEntity.ok(userDTOService.getRecommandIllustrationByUserId(userId));
    }

    @GetMapping("/get-my-sub-ser-lv")
    public ResponseEntity<?> getMySubSerLv(@RequestParam String token) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }

        Long userId = jwtService.getUserIdFromToken(token);
        var result = subLevelPricingService.getSub(userId);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/change-sub-service")
    public ResponseEntity<?> changeSubSerivceLevel(@RequestParam String token, @RequestBody(required = false) List<SubLevelPricing> newSubInfo) {
        // 处理用户设置的自己的订阅等级。
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        try {
            User user = new User();
            Long userId = jwtService.getUserIdFromToken(token);
            user.setId(userId);
            if (newSubInfo == null) {
                user.setSubsLevelForFans(0);
                userService.updateById(user);
                return ResponseEntity.ok("Stop Sub successfully");
            }

            
            var oldList = subLevelPricingService.getSub(userId);
            if (oldList != null) {
                for (var i : oldList) {
                    subLevelPricingService.removeById(i);
                }
            }
            for (var i : newSubInfo) {
                i.setUserId(userId);
                subLevelPricingService.save(i);
            }
            user.setSubsLevelForFans(newSubInfo.size());
            userService.updateById(user);

            return ResponseEntity.ok(newSubInfo);


        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @PostMapping("/change-sub-level")
    public ResponseEntity<?> changeSubLevel(@RequestParam String token, @RequestBody String newSubInfo) {
        // 处理用户订阅别人的等级
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        try {
            Long userId = jwtService.getUserIdFromToken(token);
            ObjectMapper objectMapper = new ObjectMapper();
            SubLevel newSubLevel = objectMapper.readValue(newSubInfo, SubLevel.class);
            newSubLevel.setSubscriberId(userId);
            var oldSubLevel = subLevelService.getSublevel(userId, newSubLevel.getSubscribedId());
            if (oldSubLevel == null) {
                subLevelService.save(newSubLevel);
            } else if (newSubLevel.getSubscriptionLevel() == 0) {
                subLevelService.removeById(oldSubLevel);
            } else {
                oldSubLevel.setSubscriptionLevel(newSubLevel.getSubscriptionLevel());
                subLevelService.updateById(oldSubLevel);
            }
            return ResponseEntity.ok(newSubLevel);          
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }
    }
    


}